﻿using System;
using OverStore.Runtime.Storage;
using OverStore.Runtime.ObjectRepository;

namespace OverStore.TutorialAsTest.UnitTest.Components
{
    sealed class FunctionalRegistry<TInstance, TIdentifier> : IEntityRegistry<TInstance, TIdentifier>
        where TInstance : class
    {
        private readonly Func<TInstance, TIdentifier> _GetIdentifierFunction;
        private readonly Func<TIdentifier, TypedRecord, TInstance> _GetObjectFunction;
        private readonly Action<TInstance, TIdentifier> _UpdateIdentifierFunction;

        public FunctionalRegistry(Func<TIdentifier, TypedRecord, TInstance> getObject, Func<TInstance, TIdentifier> getIdentifier)
        {
            if (getObject == null)
                throw new ArgumentNullException("getObject");
            if (getIdentifier == null)
                throw new ArgumentNullException("getIdentifier");
            _GetObjectFunction = getObject;
            _GetIdentifierFunction = getIdentifier;
        }

        public FunctionalRegistry(Func<TIdentifier, TypedRecord, TInstance> getObject, Func<TInstance, TIdentifier> getIdentifier, Action<TInstance, TIdentifier> updateIdentifier)
            : this(getObject, getIdentifier)
        {
            if (updateIdentifier == null)
                throw new ArgumentNullException("updateIdentifier");
            _UpdateIdentifierFunction = updateIdentifier;
        }

        public TIdentifier GetIdentifier(TInstance instance)
        {
            return _GetIdentifierFunction(instance);
        }

        public TInstance GetInstance(TIdentifier identifier)
        {
            return _GetObjectFunction(identifier, null);
        }

        public TInstance GetInstanceOrDefault(TIdentifier identifier)
        {
            return _GetObjectFunction(identifier, null);
        }

        public TInstance GetInstance(TIdentifier identifier, TypedRecord data)
        {
            return _GetObjectFunction(identifier, data);
        }

        public void PutNewIdentifier(TInstance instance, TIdentifier identifier)
        {
            if (_UpdateIdentifierFunction != null)
                _UpdateIdentifierFunction(instance, identifier);
        }

        public Type Type
        {
            get { return typeof(TInstance); }
        }


        public bool Reload(TInstance instance)
        {
            throw new NotImplementedException();
        }

        public void Save(TInstance instance)
        {
            throw new NotImplementedException();
        }

        public void AcceptChanges(TInstance instance)
        {
            throw new NotImplementedException();
        }

        public void MarkAsDeleted(TInstance instance)
        {
            throw new NotImplementedException();
        }

        public void MarkAsDirty(TInstance instance)
        {
            throw new NotImplementedException();
        }

        public void Add(TInstance instance)
        {
            throw new NotImplementedException();
        }

        public TInstance Attach(TInstance instance)
        {
            throw new NotImplementedException();
        }

        public void SaveAll()
        {
            throw new NotImplementedException();
        }

        public System.Collections.Generic.IEnumerable<TInstance> ExecuteObjectReader<TParameter>(TParameter parameter)
            where TParameter : class
        {
            throw new NotImplementedException();
        }

        public System.Collections.Generic.IEnumerable<TInstance> ExecuteObjectReader()
        {
            throw new NotImplementedException();
        }

        #region IRepositoryBase Members


        public void AcceptAllChanges()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}